package com.udistrital.framework.util;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public final class ParametrosSistema {

	private static SupportedLocales currentLocale = SupportedLocales.es_CO;

	private static Map<BaseNames, ResourceBundle> bundles = new HashMap<BaseNames, ResourceBundle>(
			BaseNames.values().length);

	public final static String obtenerValorPropidedad(BaseNames basenames,
			SupportedLocales locale, String key, String... messageArguments) {
		
		ResourceBundle bundle;
		if (!bundles.containsKey(basenames)) {
			synchronized (bundles) {
				bundle = ResourceBundle.getBundle(basenames.getBasenameFile(), locale.getLocale());
				bundles.put(basenames, bundle);	
			}
		} else {
			bundle = bundles.get(basenames);
		}

		String message = bundle.getString(key);
		if (messageArguments.length > 0) {
			return formatMessage(message, messageArguments);
		} else {
			return message;
		}
	}

	private static String formatMessage(String template, Object[] messageArguments) {
		MessageFormat formatter = new MessageFormat(template);
		formatter.setLocale(currentLocale.getLocale());
		return formatter.format(messageArguments);
	}
	
	public final static String obtenerValorPropidedad(BaseNames basenames,
			String key, String... messageArguments) {
		return obtenerValorPropidedad(basenames, currentLocale, key, messageArguments);
	}

	public enum SupportedLocales {
		es_CO("es", "CO"), en_US("en", "US");

		private Locale locale;

		SupportedLocales(String a, String b) {
			locale = new Locale(a, b);
		}

		public Locale getLocale() {
			return locale;
		}
	}

	/**
	 * ENUM Properties
	 * @author ccadavid
	 *
	 */
	public enum BaseNames {
		
		labels("asctcWeb"),
		parametros("parametros"),
		parametros_as400("parametros_as400"),
		parametros_connetion("application-connection"),
		parametros_ldap("ldap"),
		messages_business("messages_business");

		private String basenameFile;

		BaseNames(String basenameFile) {
			this.basenameFile = basenameFile;
		}

		public String getBasenameFile() {
			return basenameFile;
		}
	}

}
